package cn.ekuma.data.dao;

import java.util.List;

import cn.ekuma.data.dao.bean.CustomFilterBean;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class CustomFilterBeanDAO extends BaseDAO<CustomFilterBean>{

	public CustomFilterBeanDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "APP_CUSTOM_FILTER",
                new Field[]{
				     new Field("ID",Datas.STRING,Formats.STRING),
                     new Field("USERID",Datas.STRING,Formats.STRING),
                     new Field("OBJECTTABLENAME",Datas.STRING,Formats.STRING),
                     new Field("OBJECTTABLENAMEDESC",Datas.STRING,Formats.STRING),
                     new Field("VALUE1",Datas.STRING,Formats.STRING)   
                    },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, CustomFilterBean obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getUserId());
		dp.setString(3,obj.getObjTableName());
		dp.setString(4,obj.getObjTableNameDesc());
		dp.setString(5,obj.getValue());
	}

	@Override
	public Class getSuportClass() {
		return CustomFilterBean.class;
	}

	@Override
	public CustomFilterBean readValues(DataRead dr, CustomFilterBean obj)
			throws BasicException {
		if(obj==null)
			obj=new CustomFilterBean();
		obj.setId(dr.getString(1));
		obj.setUserId(dr.getString(2));
		obj.setObjTableName(dr.getString(3));
		obj.setObjTableNameDesc(dr.getString(4));
		obj.setValue(dr.getString(5));
		return obj;
	}
	
	
	public List<CustomFilterBean> listCustomFilterBeanByUserAndTableName(final String userId,final String tableName) throws BasicException{
		 return new PreparedSentence(s
	                , getTableDefinition().getListSQL()+" WHERE USERID = ? AND OBJECTTABLENAME=?"
	                , SerializerWriteParams.INSTANCE
	                ,this).list(new DataParams() { 
	                	public void writeValues() throws BasicException {
	                        setString(1, userId); 
	                        setString(2, tableName);
	                }});
	}
	
}
